﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using CityDashboard.Models;
using Windows.UI;
using Windows.UI.Xaml.Shapes;
using System.Collections.ObjectModel;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236
using CityDashboard.ViewModels;
using Microsoft.Practices.Prism.Mvvm.Interfaces;
using Microsoft.Practices.Unity;

namespace CityDashboard.Views
{
    public sealed partial class Top5InvestorChartSection : UserControl
    {

        public static void TopInvestorListChangedCallback(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            var list = (ThongKeTop5NhaDT)args.NewValue;
            double maxNumber = list.Max(x => x.Number);

            int i = 0;
            foreach (var topInvestorModel in list)
            {
                topInvestorModel.Percentage = (double)topInvestorModel.Number / maxNumber;
                topInvestorModel.Color = GetBrushOf(i);
                i++;
            }
        }
        public static Brush GetBrushOf(int index)
        {
            Brush[] brushList = new Brush[]{
                new SolidColorBrush(new Color{ R=0, G=165, B=210, A=255}),
                new SolidColorBrush(new Color{R=9, G=122, B=178, A=255}),
                new SolidColorBrush(new Color{R=38, G=102, B=164, A=255}),
                new SolidColorBrush(new Color{R=53, G=86, B=155, A=255}),
                new SolidColorBrush(new Color{R=43, G=67, B=127, A=255})
            };
            index = index % 5;
            return brushList[index];
        }





        public ThongKeTop5NhaDT TopInvestorList
        {
            get { return (ThongKeTop5NhaDT)GetValue(TopInvestorListProperty); }
            set { SetValue(TopInvestorListProperty, value); }
        }

        // Using a DependencyProperty as the backing store for TopInvestorList.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TopInvestorListProperty =
            DependencyProperty.Register("TopInvestorList", typeof(ThongKeTop5NhaDT), typeof(Top5InvestorChartSection), new PropertyMetadata(null, TopInvestorListChangedCallback));


        public Top5InvestorChartSection()
        {
            this.InitializeComponent();
        }

        /*private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            var nav = (INavigationService)(App.Current as App).Container.Resolve(typeof(INavigationService));
            nav.Navigate("TopNhaDauTu", TopInvestorList.Top5DN);
        }
        */
        //TopNDT
        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            var nav = (INavigationService)(App.Current as App).Container.Resolve(typeof(INavigationService));
            nav.Navigate("TopNhaDauTu", TopInvestorList.Top5NDT);
        }

        /*private void UIElement_OnTapped(object sender, TappedRoutedEventArgs e)
        {
            var index = TopInvestorList.IndexOf(((sender as Grid).DataContext as TopInvestorModel));
            var nav = (INavigationService)(App.Current as App).Container.Resolve(typeof(INavigationService));
            nav.Navigate("NhaDauTu", MainViewModel.ThongKeData.Top5DN[index]);
        }
        */
        //TopNDT
        private void UIElement_OnTapped(object sender, TappedRoutedEventArgs e)
        {
            var index = TopInvestorList.IndexOf(((sender as Grid).DataContext as TopInvestorModel));
            var nav = (INavigationService)(App.Current as App).Container.Resolve(typeof(INavigationService));
            nav.Navigate("NhaDauTu", MainViewModel.ThongKeData.Top5NDT[index]);
        }

    }
}
